package com.shanxia.im.sdk.client;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.cmd.Cmd106;
import com.shanxia.im.sdk.client.constant.CIMConstant;
import com.shanxia.im.sdk.client.model.Intent;

/**
 * CIM 功能接口
 */
public class CIMPushManager {

	static String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";

	static String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION";

	static String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY";

	static String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION";

	static String ACTION_DESTROY = "ACTION_DESTROY";

	/**
	 * 销毁的
	 */
	public static final int STATE_DESTROYED = 0x0000DE;
	/**
	 * 停止推送
	 */
	public static final int STATE_STOPPED = 0x0000EE;
	/**
	 * 正常
	 */
	public static final int STATE_NORMAL = 0x000000;

	/**
	 * 初始化,连接服务端，在程序启动页或者 在Application里调用
	 * 
	 * @param ip
	 * @param port
	 */
	public static void connect(String ip, int port) {

		CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_CIM_DESTROYED, false);
		CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_MANUAL_STOP, false);

		CIMCacheManager.getInstance().putString(CIMCacheManager.KEY_CIM_SERVER_HOST, ip);
		CIMCacheManager.getInstance().putInt(CIMCacheManager.KEY_CIM_SERVER_PORT, port);

		Intent serviceIntent = new Intent();
		serviceIntent.putExtra(CIMCacheManager.KEY_CIM_SERVER_HOST, ip);
		serviceIntent.putExtra(CIMCacheManager.KEY_CIM_SERVER_PORT, port);
		serviceIntent.setAction(ACTION_CREATE_CIM_CONNECTION);
		startService(serviceIntent);

	}

	private static void startService(Intent intent) {
		CIMPushService.getInstance().onStartCommand(intent);
	}

	protected static void connect() {

		boolean isManualStopped = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
		boolean isManualDestroyed = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);

		if (isManualStopped || isManualDestroyed) {
			return;
		}

		String host = CIMCacheManager.getInstance().getString(CIMCacheManager.KEY_CIM_SERVER_HOST);
		int port = CIMCacheManager.getInstance().getInt(CIMCacheManager.KEY_CIM_SERVER_PORT);

		connect(host, port);

	}

	public static void bindAccount(String account, int sign, int interval, int pileNum, int chargingRecordCount) {

		boolean isManualDestroyed = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
		if (isManualDestroyed || account == null || account.trim().length() == 0) {
			return;
		}
		sendBindRequest(account, sign, interval, pileNum, chargingRecordCount);

	}

	// 签到指令
	private static void sendBindRequest(String account, int sign, int interval, int pileNum, int chargingRecordCount) {

		CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_MANUAL_STOP, false);

		Cmd106 cmd106 = new Cmd106();
		cmd106.setSn(account);
		cmd106.setSign(sign);
		cmd106.setSoftwareVer("100.00");
		cmd106.setSignInterval(interval);
		cmd106.setPileNum(pileNum);
		cmd106.setChargingRecordCount(chargingRecordCount);
		LocalDateTime localDateTime = LocalDateTime.now();
		DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		cmd106.setCurrentTime(localDateTime.format(dtf2));// 设置模拟充电桩的标准时间
		sendRequest(cmd106);
	}

	/**
	 * 发送一个CIM请求
	 * 
	 * @body
	 */
	public static void sendRequest(ProtocolDataBase protocolDataBase) {

		boolean isManualStopped = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
		boolean isManualDestroyed = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);

		if (isManualStopped || isManualDestroyed) {
			return;
		}

		Intent serviceIntent = new Intent();
		//serviceIntent.putExtra(SentBody.class.getName(), body);
		serviceIntent.setAction(ACTION_SEND_REQUEST_BODY);
		serviceIntent.setProtocolDataBase(protocolDataBase);
		startService(serviceIntent);

	}

	/**
	 * 停止接受推送，将会退出当前账号登录，端口与服务端的连接
	 * 
	 */
	public static void stop() {

		boolean isManualDestroyed = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
		if (isManualDestroyed) {
			return;
		}

		CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_MANUAL_STOP, true);

		startService(new Intent(ACTION_CLOSE_CIM_CONNECTION));

	}

	/**
	 * 完全销毁CIM，一般用于完全退出程序，调用resume将不能恢复
	 */
	public static void destroy() {

		CIMCacheManager.getInstance().putBoolean(CIMCacheManager.KEY_CIM_DESTROYED, true);

		Intent serviceIntent = new Intent();
		serviceIntent.setAction(ACTION_DESTROY);
		startService(serviceIntent);

	}

	/**
	 * 重新恢复接收推送，重新连接服务端，并登录当前账号如果autoBind == true
	 */
	public static void resume() {

		boolean isManualDestroyed = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
		if (isManualDestroyed) {
			return;
		}

		autoBindDeviceId();
	}

	protected static boolean autoBindDeviceId() {

		String account = getAccount();

		boolean isManualDestroyed = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
		boolean isManualStopped = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
		if (isManualStopped || account == null || account.trim().length() == 0 || isManualDestroyed) {
			return false;
		}

		sendBindRequest(account, 0, 60, 1, 5);

		return true;
	}

	public static boolean isConnected() {
		return CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_CONNECTION_STATE);
	}

	public static int getState() {
		boolean isManualDestroyed = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_CIM_DESTROYED);
		if (isManualDestroyed) {
			return STATE_DESTROYED;
		}

		boolean isManualStopped = CIMCacheManager.getInstance().getBoolean(CIMCacheManager.KEY_MANUAL_STOP);
		if (isManualStopped) {
			return STATE_STOPPED;
		}

		return STATE_NORMAL;
	}


	public static String getClientVersion() {
		return System.getProperties().getProperty(CIMConstant.ConfigKey.CLIENT_VERSION);
	}

	public static String getAccount() {
		return System.getProperties().getProperty(CIMConstant.ConfigKey.CLIENT_ACCOUNT);
	}

	public static void setAccount(String account) {
		System.getProperties().put(CIMConstant.ConfigKey.CLIENT_ACCOUNT, account);
	}

	public static void setClientVersion(String version) {
		System.getProperties().put(CIMConstant.ConfigKey.CLIENT_VERSION, version);
	}
 
	private static String getDeviceId() {
		
		String currDeviceId = System.getProperties().getProperty(CIMConstant.ConfigKey.CLIENT_DEVICE_ID);
		if(currDeviceId != null) {
			return currDeviceId;
		}
		String deviceId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		System.getProperties().put(CIMConstant.ConfigKey.CLIENT_DEVICE_ID, deviceId);
		return deviceId;
	}
}
